from typing import *
from heapq import heapify, heappop


# enumerate 可以指定起始坐标，简化越界处理
class Solution:

    def findScore(self, nums: List[int]) -> int:
        n = len(nums)
        mark = [False] * n
        q = [(v, i) for i, v in enumerate(nums)]
        heapify(q)
        ans = 0
        while q:
            v, i = heappop(q)
            if not mark[i]:
                ans += v
                mark[i] = True
                if i + 1 < n and not mark[i + 1]:
                    mark[i + 1] = True
                if i - 1 >= 0 and not mark[i - 1]:
                    mark[i - 1] = True
        return ans


nums = [2, 3, 5, 1, 3, 2]

s = sorted(enumerate(nums, 1), key=lambda p: p[1])
for v in s:
    print(s)


class Solution:

    def findScore(self, nums: List[int]) -> int:
        n = len(nums)
        v = [False] * n
        ans = 0
        for i, num in sorted(enumerate(nums), key=lambda p: (p[1], p[0])):
            if not v[i]:
                v[i] = True
                ans += num
                if i > 0:
                    v[i - 1] = True
                if i < n - 1:
                    v[i + 1] = True
        return ans


class Solution:

    def findScore(self, nums: List[int]) -> int:
        n = len(nums)
        v = [False] * n
        ans = 0
        for i, num in sorted(enumerate(nums, 1), key=lambda p: (p[1], p[0])):
            if not v[i]:
                v[i] = True
                ans += num
                if i > 0:
                    v[i - 1] = True
                if i < n - 1:
                    v[i + 1] = True
        return ans